package com.thumbnail;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import net.coobird.thumbnailator.Thumbnails;

@Service
public class ThumbnailService {
	public static final int WIDTH = 100;
	public static final int HEIGHT = 100;
	
	public String thumbnail(CommonsMultipartFile file, String uploadPath, String realUploadPath) {
		
		try {
			String des = realUploadPath + "/thumb_" + file.getOriginalFilename();
			/*
			 * Thumbnailator 是一个用来生成图像缩略图的 Java 类库，通过很简单的代码即可生成图片缩略图
			 */
			Thumbnails.of(file.getInputStream()).size(WIDTH,HEIGHT).toFile(des);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return uploadPath + "/thumb_" + file.getOriginalFilename();
	}

}
